<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="客户名称" prop="customer_id">
                        <el-input v-model="customer_name" placeholder="请输入客户名称" readonly @click="checkCustom"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="会议名称" prop="metting">
                        <el-input v-model="form.metting" placeholder="请输入会议名称" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="项目日期" prop="project_time">
                        <el-date-picker v-model="form.project_time" type="date" placeholder="请选择日期" style="width: 100%;" :value-format="'YYYY-MM-DD'" @change="timeChange"></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="项目结束日期" prop="project_end_time">
                        <el-date-picker v-model="form.project_end_time" type="date" placeholder="请选择日期" style="width: 100%;" :value-format="'YYYY-MM-DD'" :disabled="!form.project_time" :disabledDate="disabledEndDate"></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="类型" prop="type">
                        <el-select v-model="form.type" placeholder="请选择类型" style="width:100%" clearable filterable>
                            <el-option v-for="item in typeList" :key="item.id" :label="item.title" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="宴会厅" prop="banquet_id">
                        <el-input v-model="banquet_name" readonly placeholder="请选择宴会厅" @click="checkBanquet"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="推介人" prop="recommend_id">
                        <el-input v-model="referrer_name" readonly placeholder="请选择推介人" @click="checkReferrer"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="项目总价" prop="total_price">
                        <el-input v-model="form.total_price" placeholder="请输入项目总价" type="number" @mousewheel.prevent @focus="onFocus($event)" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="是否开票" prop="is_ticket">
                        <el-switch v-model="form.is_ticket" :active-value="1" :inactive-value="0"></el-switch>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" clearable></el-input>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <cus-list v-if="dialog.custom" ref="customDialog" @success="customBack" @closed="dialog.custom = false"></cus-list><!-- 客户选择 -->
    <select-banquet v-if="dialog.banquet" ref="banquetDialog" @success="banquetBack" @closed="dialog.banquet = false"></select-banquet><!-- 宴会厅选择 -->
    <select-referrer v-if="dialog.referrer" ref="referrerDialog" @success="referrerBack" @closed="dialog.referrer = false"></select-referrer><!-- 推介人选择 -->
</template>

<script>
import cusList from '@/views/client/list/cusList.vue';
import SelectBanquet from '@/views/product/banquet/selectBanquet.vue';
import SelectReferrer from '@/views/product/referrer/selectReferrer.vue';
export default {
  components: { cusList, SelectBanquet, SelectReferrer },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增项目',
                client: '新增项目',
				edit: '编辑项目',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id:"",
                project_code:"",
				customer_id: "",
                metting:"",
                project_time:this.moment().format('YYYY-MM-DD'),
                project_end_time: "",
                type:"",
                banquet_id:"",
                recommend_id:"",
                total_price:"",
                is_ticket:"",
                remark:"",
			},
            customer_name:"",   //客户名称
            banquet_name:"",    //宴会厅名称
            referrer_name:"",   //推介人
			//验证规则
			rules: {
				customer_id: [{ required: true, message: '请选择客户' }],
				metting: [{ required: true, message: '请输入会议名称' }],
				project_time: [{ required: true, message: '请选择项目日期' }],
				recommend_id: [{ required: true, message: '请选择推介人' }],
				total_price: [{ required: true, message: '请输入项目总价' }],
			},
            dialog:{
                custom:false,
                banquet:false,
                referrer:false,
            },
            typeList:[
                {id:1,title:"会议"},
                {id:2,title:"婚礼"},
                {id:3,title:"会议代收"},
                {id:4,title:"会议代收+自付"},
                {id:5,title:"赞助"},
            ],
            is_client:false,
            date:this.moment().format('YYYY-MM-DD'),
            minEndDate: "",
		}
	},
	mounted() {
        this.timeChange(this.form.project_time)
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.project.project.edit.post(this.form);
					} else {
						res = await this.$API.project.project.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
                        this.$emit('success', this.form, this.mode)
                        this.visible = false;
                        this.$message.success("操作成功")
                        // if(this.mode == 'add'){
                        //     let schedule_obj = {
                        //         title: this.form.project_time + this.form.metting,
                        //         date: this.date,
                        //         content: this.form.remark,
                        //     }
                        //     await this.$API.basic.plan.add.post(schedule_obj)
                        // }
						// setTimeout(() => { 
                        //     this.$emit('success', this.form, this.mode)
                        //     this.visible = false;
                        //     this.$message.success("操作成功")
						// }, 100)
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data) {
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
            this.form.id = data.id
            this.form.project_code = data.project_code
            this.form.customer_id = data.customer_id
            this.form.metting = data.metting
            this.form.project_time = data.project_time
            // this.form.project_end_time = this.moment(data.project_end_time * 1000 ).format('YYYY-MM-DD')
            this.form.project_end_time = data.project_end_time
            this.form.type = data.type
            this.form.banquet_id = data.banquet_id
            this.form.recommend_id = data.recommend_id
            this.form.total_price = data.total_price
            this.form.is_ticket = data.is_ticket
            this.form.remark = data.remark
            this.customer_name = data.customer.company_name
            this.banquet_name = data.banquet.banquet_name
            this.referrer_name = data.recommend.recommend_name
		},
        // 客户新增
        customData(data){
            this.is_client = true
            this.form.customer_id = data.customer_id
            this.customer_name = data.customer_name
        },
        onFocus(event){
            event.target.select();
        },
        // 客户选择
        checkCustom(){
            if(!this.is_client){
                this.dialog.custom = true
                this.$nextTick(() => {
                    this.$refs.customDialog.open('add')
                })
            }
        },
        customBack(data){
            this.form.customer_id = data.id
            this.customer_name = data.company_name
        },
        // 宴会厅选择
        checkBanquet(){
            this.dialog.banquet = true
			this.$nextTick(() => {
				this.$refs.banquetDialog.open('add')
			})
        },
        banquetBack(data){
            this.form.banquet_id = data.id
            this.banquet_name = data.banquet_name
        },
        // 推介人选择
        checkReferrer(){
            this.dialog.referrer = true
			this.$nextTick(() => {
				this.$refs.referrerDialog.open('add')
			})
        },
        referrerBack(data){
            this.form.recommend_id = data.id
            this.referrer_name = data.recommend_name
        },
        timeChange(date){
            this.minEndDate = date ? new Date(date) : null;
			// 如果结束日期早于新的开始日期，则重置结束日期
			if (this.form.project_end_time && new Date(this.form.project_end_time) < new Date(date)) {
				this.form.project_end_time = null;
			}
        },
        /**结束日期禁用规则*/
		disabledEndDate(time) {
			if (!this.minEndDate) return false;
			const minDate = new Date(this.minEndDate);
			minDate.setHours(0, 0, 0, 0);
			const current = new Date(time);
			current.setHours(0, 0, 0, 0);
			return current < minDate;
		},
	}
}
</script>

<style></style>
